<?php
/**
 * 问卷管理模块
 * @author ChunYang.Jiang<chunyang@staff.sina.com.cn>
 * @copyright sina
 * @version $Id$
 */
class Survey extends Controller
{
	public function __construct()
	{
		parent::__construct();
		$tabs = array(
			array(
				'name'	=> '创建问卷',
				'url'	=> '/Admin/Survey/Add',
			),
			array(
				'name'	=> '查看问卷',
				'url'	=> '/Admin/Survey/Show',
			),
			array(
				'name'	=> '正在进行的问卷',
				'url'	=> '/Admin/Survey/Show/status/1',
			),
			array(
				'name'	=> '已结束问卷',
				'url'	=> '/Admin/Survey/show/status/2',
			),
		);
		$this->assign('tabs', $tabs);
	}
	public function Add()
	{
		$fck = Util::getFck('memo');
		$fck->Width = '80%';
		$fck->Height = 300;
		$memo = $fck->CreateHtml();
		$fck->InstanceName = 'endnotice';
		$endnotice = $fck->CreateHtml();
		$this->assign('memo', $memo);
		$this->assign('endnotice', $endnotice); 
		$this->display('Survey/Add.html');
	}
	 
	public function doAdd()
	{
		$result = $this->_chkFields(array('title'));
		if (true === $result){
			$row['title'] = $_POST['title'];
			$row['endtime'] = isset($_POST['endtime']) && $_POST['endtime'] ? strtotime($_POST['endtime']) : 0;
			$row['createtime'] = time();
			$row['memo'] = $_POST['memo'];
			$row['tags'] = $_POST['tags'];
			$row['limitmethod'] = isset($_POST['limitmethod']) ? $_POST['limitmethod'] : '';
			$row['endaction'] = isset($_POST['endaction']) ? $_POST['endaction'] : '';
			$row['endnotice'] = isset($_POST['endnotice']) ? $_POST['endnotice'] : '';
			if (SurveyModule::add($row)){
				Util::msgRedirect('添加成功', '/Admin/Survey');
			}else{
				Util::msgRedirect('操作失败');
			}
		}else{
			Util::msgRedirect($result);
		}
	}
	
	public function Edit()
	{
		$id = isset($_GET['id']) && $_GET['id'] ? intval($_GET['id']) : Util::msgRedirect('缺少参数');
		$survey = SurveyModule::getSurveyById($id);
		if (!$survey) {
			Util::msgRedirect('问卷不存在');
		}
		$fck = Util::getFck('memo');
		$fck->Width = '80%';
		$fck->Height = 300;
		$fck->Value = $survey['memo'];
		$memo = $fck->CreateHtml();
		$fck->InstanceName = 'endnotice';
		$fck->Value = $survey['endnotice'];
		$endnotice = $fck->CreateHtml();
		$this->assign('memo', $memo);
		$this->assign('endnotice', $endnotice);
		
		$this->assign('survey', $survey);
		
		$this->display('Survey/edit.html');
	}
	
	public function updStatus()
	{
		$id = isset($_GET['id']) && $_GET['id'] ? intval($_GET['id']) : Util::msgRedirect('请选择问卷');
		$status = isset($_GET['status']) && is_numeric($_GET['status']) ? intval($_GET['status']) : Util::msgRedirect('缺少参数');
		if (!in_array($status, array(0,1,2,3,4))) {
			Util::msgRedirect('错误的状态码'.$status);
		}
		if (!SurveyModule::getSurveyById($id)){
			Util::msgRedirect('问卷不存在');
		}
		$row['status'] = $status;
		if (SurveyModule::update($id, $row)){
			Util::msgRedirect('操作成功', '/Admin/Survey');
		}else{
			Util::msgRedirect('操作失败');
		}
	}
	
	public function Show()
	{
		$total = 0;
		$pageSize = 6;
		$page = isset($_GET['page']) && $_GET['page'] && $_GET['page']>0 ? intval($_GET['page']) : 1;
		$start = ($page-1) * $pageSize;
		$surveyList = SurveyModule::getSurvey('1', $start, $pageSize, $total);
		foreach ($surveyList as &$item){
			if ($item['time']){
				
			}
		}
		Util::page($total, $pageSize, $this->tpl);
		$this->assign('surveyList', $surveyList);
		$this->display('Survey/Show.html');
	}
	
	private function _chkFields($fields)
	{
		if (in_array('title', $fields)){
			if (!isset($_POST['title']) || !$_POST['title']){
				return '缺少问卷标题';
			}
		}
		return true;
	}
}